# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QMessageBox
from PyQt5.QtCore import Qt


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt5 Window Modality Example")
        self.setGeometry(100, 100, 300, 200)

        # 创建一个标签和两个按钮
        self.label = QLabel("This is a modal window", self)
        self.label.setGeometry(50, 50, 200, 30)

        self.showModalButton = QPushButton("Show Modal Window", self)
        self.showModalButton.setGeometry(50, 100, 200, 30)
        self.showModalButton.clicked.connect(self.showModalWindow)

        self.showNonModalButton = QPushButton("Show Non-Modal Window", self)
        self.showNonModalButton.setGeometry(50, 150, 200, 30)
        self.showNonModalButton.clicked.connect(self.showNonModalWindow)

    def showModalWindow(self):
        # 创建一个模态窗口
        modalWindow = QMessageBox(self)
        modalWindow.setWindowTitle("Modal Window")
        modalWindow.setText("This is a modal window.")
        modalWindow.addButton(QMessageBox.Ok)
        modalWindow.setWindowModality(Qt.WindowModal)
        modalWindow.exec_()

    def showNonModalWindow(self):
        # 创建一个非模态窗口
        nonModalWindow = QMessageBox(self)
        nonModalWindow.setWindowTitle("Non-Modal Window")
        nonModalWindow.setText("This is a non-modal window.")
        nonModalWindow.addButton(QMessageBox.Ok)
        nonModalWindow.setWindowModality(Qt.ApplicationModal)
        nonModalWindow.show()


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()